<?php
class ControllerModuleBds extends Controller
{
	public function __construct() 
	{
		$this->load->model("addon/bds");
		$this->load->model("addon/nhom");
		$this->load->model("core/sitemap");
		$this->load->helper('image');
	}
	
	public function getList($sitemapid="", $count = 10,$headername ="", $template = array(),$bds=array())
	{
		$siteid = $this->user->getSiteId();
		$this->data['sitemap'] = $this->model_core_sitemap->getItem($sitemapid, $siteid);
		$donvi = 1000000;
		$where = "";
		
		$para = $this->string->referSiteMapToArray($_GET['search']);
		if(count($para))
		{
			foreach($para as $val)
			{
				$ar = split("=",$val);
				$datasearch[$ar[0]] = $ar[1];	
			}
			
			$_GET = $datasearch;
			$arr = array();
			
			foreach($datasearch as $key => $item)
			{
				if($item !="" && $key != "mucgia")
					$arr[] = " AND " . $key ." = '".$item."'";
					
				if($key == "mucgia")
				{
					$arrgia = split("-",$item);
					$giatu = (int)$arrgia[0];
					$giaden = (int)$arrgia[1];
					if($giatu)
						$arr[] = " AND gia >= '".$giatu*$donvi ."'";
					if($giaden)
						$arr[] = " AND gia <= '".$giaden*$donvi ."'";
				}
			}
			
			
			
			$where = implode("",$arr);
			
		}
		
		$this->data['datas'] = array();
		$where.=" ORDER BY `dlbds`.`statusdate` DESC";
		$rows = $this->model_addon_bds->getList($where);
		//Page
		$page = $this->request->get['page'];		
		$x=$page;		
		$limit = $count;
		$total = count($rows); 
		$uri = "site/".$siteid."/".$sitemapid."/";
		// work out the pager values 
		$this->data['pager']  = $this->pager->pageLayoutWeb($total, $limit, $page,$uri); 
		
		$pager  = $this->pager->getPagerData($total, $limit, $page); 
		$offset = $pager->offset; 
		$limit  = $pager->limit; 
		$page   = $pager->page;
		for($i=$offset;$i < $offset + $limit && count($rows[$i])>0;$i++)
		{
			$this->data['datas'][$i] = $rows[$i];
			if($this->data['datas'][$i]['imagepath']=="")
				$this->data['datas'][$i]['imagepath'] ="default.jpg";
			$this->data['datas'][$i]['imagethumbnail'] = HelperImage::fixsize($this->data['datas'][$i]['imagepath'], $template['width'], $template['height']);
			$link = HTTP_SERVER."site/".$siteid."/sangiaodich/".$this->data['datas'][$i]['bdsid'];
			$this->data['datas'][$i]['link'] = $link;
		}
		
		$this->id="news";
		$this->template=$template['template'];
		$this->render();
	
	}
	
	public function getDetail($sitemapid="", $count = 10,$headername ="", $template = array())
	{
		$siteid = $this->user->getSiteId();
		$this->data['sitemap'] = $this->model_core_sitemap->getItem($sitemapid, $siteid);
		
		$bdsid = $this->request->get['id'];
		
		$this->data['item'] = $this->model_addon_bds->getItem($bdsid);
		$this->data['item']['imagethumbnail'] = HelperImage::resizePNG($this->data['item']['imagepath'], $template['width'], $template['height']);
		$this->data['item']['imagepreview'] = HelperImage::resizePNG($this->data['item']['imagepath'], 800,800);
		
		$listfile = $this->data['item']['attachment'];
		$listfileid=array();
		if($listfile)
			$listfileid=split(",",$listfile);
			
		array_unshift($listfileid,$this->data['post']['imageid']);
		
		$this->data['subimage']=array();
		$this->data['attachment']=array();
		
		foreach($listfileid as $key => $item)
		{
			$this->data['attachment'][$key] = $this->model_core_file->getFile($item);
			if($this->string->isImage($this->data['attachment'][$key]['extension']))
			{
				$this->data['subimage'][$key]['imagethumbnail'] = HelperImage::resizePNG($this->data['attachment'][$key]['filepath'],$template['width'], $template['height']);	
				$this->data['subimage'][$key]['imagepreview'] = HelperImage::resizePNG($this->data['attachment'][$key]['filepath'],  800, 800);
			}
			
			if(!$this->string->isImage($this->data['attachment'][$key]['extension']))
				$this->data['attachment'][$key]['imagethumbnail'] = DIR_IMAGE."icon/dinhkem.png";
			
		}
		
		
		$this->id="news";
		$this->template=$template['template'];
		$this->render();
	}
	
	public function viewDetail()
	{
		$siteid = $this->user->getSiteId();
		$this->data['sitemap'] = $this->model_core_sitemap->getItem($sitemapid, $siteid);
		
		$bdsid = $this->request->get['id'];
		
		$this->data['item'] = $this->model_addon_bds->getItem($bdsid);
		$this->data['item']['imagethumbnail'] = HelperImage::resizePNG($this->data['item']['imagepath'], $template['width'], $template['height']);
		$this->data['item']['imagepreview'] = HelperImage::resizePNG($this->data['item']['imagepath'], 800,800);
		
		$listfile = $this->data['item']['attachment'];
		$listfileid=array();
		if($listfile)
			$listfileid=split(",",$listfile);
			
		array_unshift($listfileid,$this->data['post']['imageid']);
		
		$this->data['subimage']=array();
		$this->data['attachment']=array();
		
		foreach($listfileid as $key => $item)
		{
			$this->data['attachment'][$key] = $this->model_core_file->getFile($item);
			if($this->string->isImage($this->data['attachment'][$key]['extension']))
			{
				$this->data['subimage'][$key]['imagethumbnail'] = HelperImage::resizePNG($this->data['attachment'][$key]['filepath'],200, 200);	
				
			}
			
			if(!$this->string->isImage($this->data['attachment'][$key]['extension']))
				$this->data['attachment'][$key]['imagethumbnail'] = DIR_IMAGE."icon/dinhkem.png";
			
		}
		
		
		$this->id="news";
		$this->template="module/bds_tooltip.tpl";
		$this->render();
	}
	
	public function sendMessage()
	{
		$data=$this->request->post;
		$error = $this->validate($data);
		if(count($error))
		{
			foreach($error as $item)
				$this->data['output'].=$item."<br>";
		}
		else
		{
			$this->load->model("core/user");
			$bdsid = $data['bdsid'];
			$item = $this->model_addon_bds->getItem($bdsid);
			
			$user = $this->model_core_user->getItem($item['assignto']);
			
			$this->load->model("core/message");
			$message['to']= $user['userid'].",".$user['email'];
			$message['from']='"'.$data['fullname'].'" '.$data['email'];
			echo $message['title']="Lien he mua bat dong san ".$item['bdsid'];
			$message['description']="Bất động sản: ".$item['bdsid']."-".$item['tieude']."<br>";
			
			$message['description'].="Họ tên: ".$data['fullname']."<br>";
			$message['description'].="Email: ".$data['email']."<br>";
			$message['description'].="Địa chỉ: ".$data['address']."<br>";
			$message['description'].="Điện thoại: ".$data['phone']."<br>";
			$message['description'].="Nội dung: ".$data['description']."<br>";
			$message['folder']="inbox";
			$this->model_core_message->insert($message);
			
			//
			
			$mail['from'] = $data['email'];
			$mail['FromName'] = $data['fullname'];
			$mail['to'] = $user['email'];
			$mail['name'] = $user['fullname'];
			$mail['subject'] = $message['title'];
			$mail['body'] =  $message['description'];
			$this->mailsmtp->sendMail($mail);
			$this->data['output'] = "true";
		}
		$this->id="content";
		$this->template="common/output.tpl";
		$this->render();
	}
	
	private function validate($data)
	{
		$err = array();
		
		if($data['fullname'] == "")
			$err["fullname"] = "Bạn chưa nhập họ và tên";
		if($data['email'] == "")
			$err["email"] = "Email không đươc rỗng";
		else
			if ($this->validation->_checkEmail($data['email']) == false )
				$err["email"] = "Email không đúng định dạng";
		if($data['address'] == "")
			$err["address"] = "Bạn chưa nhập địa chỉ";
		if($data['phone'] == "")
			$err["phone"] = "Bạn chưa nhập số điên thoại";
		if($data['description'] == "")
			$err["description"] = "Bạn chưa nhập lời nhắn";
		return $err;
	}
}
?>